package com.helwen.tools;

import cn.hutool.core.lang.Console;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.webservice.SoapClient;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;
import org.junit.jupiter.api.Test;

import java.io.InputStream;

/**
 * @author: heqiwen
 * @date : 2021/5/26 22:33
 */
public class WebserviceWSDL {

    public static String postXmlRequestInputStream(String requestUrl, String xmlData) throws Exception{

        /*PostMethod postMethod = new PostMethod(requestUrl);
        byte[] b = xmlData.getBytes("utf-8");
        InputStream is = new ByteArrayInputStream(b, 0, b.length);
        RequestEntity re = new InputStreamRequestEntity(is, b.length, "text/xml;charset=utf-8");
        postMethod.setRequestEntity(re);

        HttpClient httpClient = new HttpClient();
        httpClient.getParams().setAuthenticationPreemptive(true);
        httpClient.getHostConfiguration().setProxy(CommonPptsUtil.get("PROXY_HOST"), Integer.valueOf(CommonPptsUtil.get("PROXY_PORT")));

        int statusCode = httpClient.executeMethod(postMethod);
        logger.debug("responseCode:"+statusCode);
        if (statusCode != 200) {
            return null;
        }*/
        //链式构建请求
        String result2 = HttpRequest.post(requestUrl)
                .header(Header.USER_AGENT, "Hutool http")//头信息，多个头信息多次调用此方法即可
                .body(xmlData.getBytes("utf-8"))//表单内容
                .timeout(20000)//超时，毫秒
                .execute().body();
        return result2;
    }

    public static void main(String[] args) {
        String xmlData =  "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:hiip=\"hiip\">\n" +
                "   <soap:Header/>\n" +
                "   <soap:Body>\n" +
                "      <hiip:PublicOperation>\n" +
                "      <hiip:payload>\n" +
                "\t        <app>EMR</app>\n" +
                "         <action>v_jhmk_pat_order</action>\n" +
                "         <message>\n" +
                "     <![CDATA[<patient_id>0002002546</patient_id>\n" +
                "        <visit_id>1</visit_id>]]>\n" +
                "        </message>\n" +
                "         </hiip:payload>\n" +
                "      </hiip:PublicOperation>\n" +
                "   </soap:Body>\n" +
                "</soap:Envelope>";
        String url = "http://192.192.2.18:7073/services/HIIPWebService?wsdl";
        SAXReader reader  = new SAXReader();
        String result = "";
        try {
            String in = postXmlRequestInputStream(url,xmlData);
            if(in!=null){
                Document doc = reader.read(in);
                result = doc.getRootElement().element("Body").element("statusWriteBackResponse").element("return").getText();
                //logger.debug("result:"+result);
            }
        } catch (Exception e) {
            //logger.error("error:",e);
            e.printStackTrace();
        }
    }

    @Test
    public void invokeTest(){
        String str="<app>EMR</app>\n" +
                "         <action>v_jhmk_pat_order</action>\n" +
                "         <message>\n" +
                "     <![CDATA[<patient_id>0002002546</patient_id>\n" +
                "        <visit_id>1</visit_id>]]>\n" +
                "        </message>";
        // 新建客户端
        SoapClient client =SoapClient.create("http://192.192.2.18:7073/services/HIIPWebService?wsdl")
// 设置要请求的方法，此接口方法前缀为web，传入对应的命名空间
                .setMethod("hiip:PublicOperation","hiip")
// 设置参数，此处自动添加方法的前缀：web
                .setParam("payload",str).setParam("app","EMR",false);
// 发送请求，参数true表示返回一个格式化后的XML内容
// 返回内容为XML字符串，可以配合XmlUtil解析这个响应
        Console.log(client.send(true));
    }
}
